﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

#region

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Applications.Web
{
    public static class RioApplicationSettings
    {
        #region Constructors

        /// <summary>
        /// Initializes the <see cref="RioApplicationSettings"/> class.
        /// </summary>
        static RioApplicationSettings()
        {
            LicenseKeyDuration = 10;
            LicenseKey = "12345";

            if (ConfigurationManager.AppSettings["LicenseKeyDuration"] != null)
            {
                LicenseKeyDuration = Convert.ToInt32(ConfigurationManager.AppSettings["LicenseKeyDuration"]);
            }

            if (ConfigurationManager.AppSettings["LicenseKey"] != null)
            {
                LicenseKey = ConfigurationManager.AppSettings["LicenseKey"].ToString();
            }
        }

        #endregion

        #region Class Properties

        public static string LicenseKey { get; set; }
        public static int LicenseKeyDuration { get; set; }

        #endregion

        #region Class Methods

        /// <summary>
        /// Saves the silverlight deployment settings.
        /// </summary>
        /// <param name="loginPage">The login page.</param>
        /// <returns></returns>
        public static string SaveSilverlightDeploymentSettings(Uri loginPage)
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;

            StringBuilder SB = new StringBuilder();
            SB.Append("<param name=\"InitParams\" value=\"");

            int SettingCount = appSettings.Count;
            for (int Idex = 0; Idex < SettingCount; Idex++)
            {
                if (!appSettings.GetKey(Idex).Equals("LicenseKey"))
                {
                    SB.Append(appSettings.GetKey(Idex));
                    SB.Append("=");
                    SB.Append(appSettings[Idex]);
                    SB.Append(",");
                }
            }

            if ((HttpContext.Current.User != null) && (!String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)))
            {
                SB.Append(String.Format("UserAccount={0},", HttpContext.Current.User.Identity.Name));
            }
            else
            {
                SB.Append(String.Format("UserAccount=,"));
                //SB.Remove(SB.Length - 1, 1);
            }

            SB.AppendFormat("AuthenticationMode={0},", SettingsManager.cStoreSettings.Authentification_Rio);
            SB.AppendFormat("LoginPage={0}", Convert.ToBase64String(Encoding.Unicode.GetBytes(loginPage.OriginalString)));

            SB.Append("\" />");

            return SB.ToString();
        }

        #endregion
    }
}